//
//  ConActivityListController.m
//  Shengzhuli
//
//  Created by FXGuo on 2017/4/18.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "ConActivityListController.h"
#import <MJRefresh.h>
#import "InfomationCell.h"
#import "ActivityNetService.h"
#import "MBProgressHUD+MJ.h"
#import "ConActivityDetailController.h"

@interface ConActivityListController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,assign) NSInteger curPage;
@property (nonatomic,strong) NSMutableArray *activities;

@end

@implementation ConActivityListController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.curPage = 1;
    self.title = @"活动列表";
    [self.view addSubview:self.tableView];
    [self getActivityList];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.activities.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    InfomationCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([InfomationCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.activityModel = self.activities[indexPath.section];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ActivityModel *model = self.activities[indexPath.section];
    ConActivityDetailController *detailVc = [[ConActivityDetailController alloc]init];
    detailVc.ID = model.ID;
    detailVc.type = WebTypeActivityDetail;
    [self.navigationController pushViewController:detailVc animated:YES];
}
- (void)getActivityList
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"pageNo"] = @(self.curPage);
    [self showClearBgWaiter];
    [ActivityNetService getActivityListsWithParame:parame response:^(BOOL success, NSString *msg, NSArray *lists) {
        [self hideWaiter];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (success) {
            [self hideRequestAgainView];
            self.tableView.mj_footer.hidden = lists.count<20;
            if (self.curPage == 1) {
                [self.activities removeAllObjects];
            }
            [self.activities addObjectsFromArray:lists];
            [self.tableView reloadData];
        }else{
            if ([msg isEqualToString:NoNetworkStatus]) {
                [self showRequestAgainView:@selector(getActivityList)];
            }else{
                [MBProgressHUD showError:msg toView:self.view];
            }
        }
    }];
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW,ScreenH) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.sectionFooterHeight = 0.01;
        _tableView.sectionHeaderHeight = 10;
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
        _tableView.rowHeight = 80;
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([InfomationCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([InfomationCell class])];
        [self.view addSubview:_tableView];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.curPage = 1;
            [self getActivityList];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            self.curPage++;
            [self getActivityList];
        }];
    }
    return _tableView;
}
- (NSMutableArray *)activities
{
    if (!_activities) {
        _activities = [NSMutableArray array];
    }
    return _activities;
}


@end
